home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8687 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: gambier.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: need for function prototypes
  5. Date: 5 Mar 1996 13:56:10 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hid9qINNapq@gambier.ugrad.cs.ubc.ca>
  8. References: <4gutho$o1a@mn5.swip.net> <4hgbo0$2fj@s02.pavilion.co.uk> <4hi462$3bf@ams.amsinc.com>
  9. NNTP-Posting-Host: gambier.ugrad.cs.ubc.ca
  10.  
  11. In article <4hi462$3bf@ams.amsinc.com>,
  12. Steve Herborn <sherborn@mail.amsinc.com> wrote:
  13. >ANSI also introduced void as meaning that the function takes no
  14. >arguments, thus removing another source of confusion.
  15.  
  16. That actually introduces a little bit of confusion. The void marker for an
  17. empty paramenter list is just there because the grammar for the parameter list
  18. does not allow it to consist of empty tokens. If you omit the void, it becomes
  19. an old-style declaration.
  20.  
  21. The grammar productions for direct-declarator are as follows (from K&R A13):
  22.  
  23. direct-declarator:
  24.     identifier
  25.     ( declarator )
  26.     direct-declarator [ constant-expression_opt ]
  27.     direct-declarator ( parameter-type-list )
  28.     direct-declarator ( identifier-list_opt )
  29.  
  30. There is nothing in the further productions for parameter-type-list that would
  31. allow an empty derivation.  However, the identifier-list_opt is clearly
  32. optional, hence it is allowed to derive an empty string.
  33.  
  34. Now, if both were allowed to derive empty strings, the grammar would be
  35. ambiguous. If the parser saw a declaration of int foo(), it would not know
  36. whether it is new style (production 3 above) or old style (production 4).
  37.  
  38. This matters, because the old-style declaration with no parameter list is just
  39. a function declaration which can stand for a definition that has arguments.
  40. The new-style declaration, on the other hand, is a prototype which asserts that
  41. the parameter list is to be empty.
  42.  
  43. The ``void'' keyword by itself is just a syntactic kludge do distinguish
  44. between the old and the new.
  45.  
  46. To see that it is a kludge, try defining a function:
  47.  
  48.     int foo(int) { }
  49.  
  50.  
  51. This is syntactically equivalent to writing int foo(void), since both int and
  52. void play the syntactic role of ``type-specifier'', and the declaration
  53. specifiers that follow the type specifier are optional.
  54.  
  55. However, the above is not allowed in a function _definition_, since the int
  56. parameter is not named. However, the void parameter is yet allowed to be
  57. without a name.
  58.  
  59. This is the ``syntactic ugliness'' referred to in the K&R2 at the end of
  60. A8.6.3.
  61. -- 
  62.  
  63.